//
// Author    : Pablo Bosch 
// Contact   : pboschvivancos@epo.org
// Last Modif: 13/03/2005
//

var MOD_DOCS = {};

var ALLDOCPNS=new Array();
var ALLDOCPNN=new Array();
var ALLDOCPNG=new Array();
var ALLDOCPNB=new Array();
var ALLDOCPNA=new Array();
var CITEDDOCS=new Array();
var CITEDDOCSLENGTH;
var ALLDOCPNALENGTH;

var seldoc="";


function docobj(idx,pn,title) {
  this._idx=idx;
  this._title=title || pn;
  this._pn=pn;
}

function DOCPNS2Doc(idx) {
  return ALLDOCPNS[idx];
}

function Doc2DOCPNS(doc) {
  //alert("Find "+doc+" "+ALLDOCPNS.length);
  for(var d in ALLDOCPNS) {
    if(ALLDOCPNS[d]==doc) {
      return d;
    }
  }
  return 0;
}

function isDocEmpty(doc) {
  return !doc || doc=="";
}

// All docs 
function InitDocPNS() {
  ALLDOCPNS=new Array("");
  for(d=1; d <= GetDocsLength() ; d++) {
    ALLDOCPNS.push(GetDocPN(d));
  }
}

// Init documents for B, use it for different Inventive Step techniques
// (mere combination, obvious choice, common design..)



function InitDocPNB() {
  ALLDOCPNB=new Array("");
  for(var d=1; d <= GetDocsLength() ; d++) {
    ALLDOCPNB.push(GetDocPN(d));
  }
  for(var d in ALLBDOCS) {
    ALLDOCPNB.push(ALLBDOCS[d]);
  }
}

function isDocpureB(doc) {
  return is_B_hint(doc);
} 	

function DOCPNB2Doc(idx) {
  return ALLDOCPNB[idx];
}

function Doc2DOCPNB(doc) {
  for(var d in  ALLDOCPNB) {
    if(ALLDOCPNB[d]==doc) {
      return d;
    }
  }
  return 0;
}


// All not hidden docs plus Empty plus "All"
function InitDocPNAll() {
  var l=0;
  ALLDOCPNA=new Array("");
  var doc_iter= new doc_iterator();
  while(!doc_iter.end()) {
    if(!doc_iter.isNotCited()) { 
	ALLDOCPNA.push(doc_iter.getPN());
        l++;
    }
    doc_iter.next();
  }
  ALLDOCPNA.push("All");
  ALLDOCPNALENGTH=l+1;
}


function doc_selection(name,seldoc,callback,opt) { 
  return html_selection(name,ALLDOCPNS,seldoc,callback,opt);
}
function docB_selection(name,seldoc,callback,opt) { 
  return  html_selection(name,ALLDOCPNB,seldoc,callback,opt);
}

// find value in greatest array (B)
function doc_selected(name) {
  return html_selected(name,ALLDOCPNB);
}

// find value in greatest array (B)
function doc_doselect(name,val) {
  return html_doselect(name,ALLDOCPNB,val);
}

function InitDocPNG() {
  ALLDOCPNG=new Array(XML_GRP.DEFGROUPDOC);
  for(d=1; d <= GetDocsLength() ; d++) {
    ALLDOCPNG.push(GetDocPN(d));
  }
}

// handle completing partial searches 

MOD_DOCS._CITEDDOCS=new Array();
MOD_DOCS.GCITEDDOCS=new Array();;
MOD_DOCS.insertciteddoc= function(cl,pn,idx,force) {
  if(pn) {
    //alert("try to insert "+pn+" as "+idx);
    // globally/previously cited
    if(!MOD_DOCS._CITEDDOCS[pn]) {              
      //alert("Check "+pn +" cited in "+GetDocCitedVersion(pn)+" as "+GetDocCitedNum(pn));
      if(!GetDocCitedNum(pn) || !GetDocCitedVersion(pn) || GetDocCitedVersion(pn) == GetClaimSetVersion()) {
	while(is_DocCitedNumUsed(idx)!="") { idx++};
	//alert("Use "+idx);
	SetDocCitedNum(pn,idx);
	MOD_DOCS._CITEDDOCS[pn]=idx++;
      } else {
	//alert(pn+" was "+GetDocCitedNum(pn));
	MOD_DOCS._CITEDDOCS[pn]=GetDocCitedNum(pn);
      }      
    }
    // cited in this comm
    if(force || IsClaimInFilter(cl)) { 
      if(!CITEDDOCS[pn]) {
	CITEDDOCS[pn]=MOD_DOCS._CITEDDOCS[pn];
	CITEDDOCSLENGTH++;
      }       
    } 
  }  
  return idx;
}


function InitCitedDocs() {
  MOD_DOCS._CITEDDOCS=new Array();
  var  _clnum=GetClaimSetLength();
  CITEDDOCS=new Array();
  CITEDDOCSLENGTH=0;
  
  var idx=GetNumCitedDocs()+1;

  /* NU doc is doc D */
  /*
  if(GetGroupsLength()>1 && GetGroupsDoc()!="A Priori") {
    idx=_insertciteddoc(0,GetGroupsDoc(),idx,true);
  } 
  */
 
  for(var cl=1; cl<= _clnum; cl++) {
    idx=MOD_DOCS.insertciteddoc(cl,GetClaimDoc_X(cl),idx);
    idx=MOD_DOCS.insertciteddoc(cl,GetClaimDoc_Y(cl),idx);
    idx=MOD_DOCS.insertciteddoc(cl,GetClaimDoc_A(cl),idx);
    if(!isDocpureB(GetClaimDoc_B(cl))) {
      idx=MOD_DOCS.insertciteddoc(cl,GetClaimDoc_B(cl),idx);
    }
  }
  ReorderDocs();
  return CITEDDOCSLENGTH; // return number of cited docs 
}

function Doc2CitedIdx(doc) {
  //alert("Doc2Cited "+doc+"="+CITEDDOCS[doc]);
  return CITEDDOCS[doc];
}


function GroupCitedDocs(g) {
    var out= new List();
    var  _clnum=GetClaimSetLength();
    var GCITEDDOCS=new Array();
    for(var cl=1; cl<= _clnum; cl++) {
      if(IsClaimInFilter(cl) && GetClaimGroup(cl)==g) {
	   GCITEDDOCS[GetClaimDoc_X(cl)]=1;          	  
           GCITEDDOCS[GetClaimDoc_Y(cl)]=1;
           GCITEDDOCS[GetClaimDoc_A(cl)]=1;
           if(!isDocpureB(GetClaimDoc_B(cl))) {
	      GCITEDDOCS[GetClaimDoc_B(cl)]=1;
           }
        }
    }
    for(d in GCITEDDOCS) {
	if(d) {
	  out.push("D"+Doc2CitedIdx(d));
        }
    }
   return out;
}
